#!/bin/bash

# 测试StackOverflowError深度修复效果
echo "=== StackOverflowError 深度修复测试 V2 ==="
echo "测试时间: $(date)"
echo ""

# 测试参数
BASE_URL="http://localhost:8080"
API_ENDPOINT="/admin-api/system/college-recommendation/recommend"

# 测试用例1: 极大数据量测试（之前导致44823条数据栈溢出）
echo "1. 极大数据量测试（重现44823条数据场景）"
echo "---------------------------------------------------"

EXTREME_PARAMS="province=辽宁&score=450&ranks=60000&year=2024&classify=物理类&subjects=&batch=本科批&pageNum=1&pageSize=500"

echo "发送极大数据量请求（可能触发44823条数据）..."
START_TIME=$(date +%s%3N)

# 使用timeout命令，避免长时间等待
timeout 60s curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${EXTREME_PARAMS}" > response.tmp 2>&1
CURL_EXIT_CODE=$?
END_TIME=$(date +%s%3N)
DURATION=$((END_TIME - START_TIME))

if [ $CURL_EXIT_CODE -eq 124 ]; then
    echo "❌ 请求超时（60秒），可能仍存在性能问题"
    echo "响应时间: >60000ms"
elif [ $CURL_EXIT_CODE -eq 0 ]; then
    HTTP_CODE=$(tail -n1 response.tmp)
    RESPONSE_BODY=$(head -n -1 response.tmp)
    
    echo "HTTP状态码: $HTTP_CODE"
    echo "响应时间: ${DURATION}ms"
    
    if [ "$HTTP_CODE" = "200" ]; then
        echo "✅ 极大数据量测试通过"
        RESULT_COUNT=$(echo "$RESPONSE_BODY" | grep -o '"total":[0-9]*' | grep -o '[0-9]*')
        echo "推荐结果数量: $RESULT_COUNT"
        
        # 检查响应时间是否合理
        if [ "$DURATION" -lt 30000 ]; then
            echo "✅ 响应时间合理 (${DURATION}ms < 30s)"
        else
            echo "⚠️  响应时间较长: ${DURATION}ms"
        fi
        
        # 检查是否有StackOverflowError
        if echo "$RESPONSE_BODY" | grep -q "StackOverflowError"; then
            echo "❌ 响应中仍包含StackOverflowError"
        else
            echo "✅ 未发现StackOverflowError"
        fi
    else
        echo "❌ 极大数据量测试失败"
        echo "响应内容: $(head -c 500 response.tmp)"
    fi
else
    echo "❌ 请求失败，curl退出码: $CURL_EXIT_CODE"
fi

rm -f response.tmp
echo ""

# 测试用例2: 内存压力测试
echo "2. 内存压力测试（连续大数据量请求）"
echo "-------------------------------------------"

SUCCESS_COUNT=0
TOTAL_REQUESTS=3

for i in $(seq 1 $TOTAL_REQUESTS); do
    echo "内存压力测试 $i/$TOTAL_REQUESTS..."
    
    START_TIME=$(date +%s%3N)
    timeout 45s curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${EXTREME_PARAMS}" > response.tmp 2>&1
    CURL_EXIT_CODE=$?
    END_TIME=$(date +%s%3N)
    DURATION=$((END_TIME - START_TIME))
    
    if [ $CURL_EXIT_CODE -eq 0 ]; then
        HTTP_CODE=$(tail -n1 response.tmp)
        if [ "$HTTP_CODE" = "200" ]; then
            SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
            echo "  请求 $i: ✅ 成功 (${DURATION}ms)"
        else
            echo "  请求 $i: ❌ 失败 HTTP $HTTP_CODE (${DURATION}ms)"
        fi
    else
        echo "  请求 $i: ❌ 超时或失败 (${DURATION}ms)"
    fi
    
    # 请求间隔，让系统恢复
    sleep 5
done

rm -f response.tmp

echo "内存压力测试结果: $SUCCESS_COUNT/$TOTAL_REQUESTS 成功"
if [ "$SUCCESS_COUNT" -eq "$TOTAL_REQUESTS" ]; then
    echo "✅ 内存压力测试通过"
else
    echo "❌ 内存压力测试失败"
fi
echo ""

# 测试用例3: 数据量递增测试
echo "3. 数据量递增测试"
echo "----------------------------"

declare -a PAGE_SIZES=(50 100 200 300 500)
declare -a SCORES=(600 550 500 450 400)

for i in "${!PAGE_SIZES[@]}"; do
    PAGE_SIZE=${PAGE_SIZES[$i]}
    SCORE=${SCORES[$i]}
    
    echo "测试数据量级别 $((i+1)): 页面大小=$PAGE_SIZE, 分数=$SCORE"
    
    TEST_PARAMS="province=辽宁&score=$SCORE&ranks=$((SCORE*100))&year=2024&classify=物理类&subjects=&batch=本科批&pageNum=1&pageSize=$PAGE_SIZE"
    
    START_TIME=$(date +%s%3N)
    timeout 30s curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${TEST_PARAMS}" > response.tmp 2>&1
    CURL_EXIT_CODE=$?
    END_TIME=$(date +%s%3N)
    DURATION=$((END_TIME - START_TIME))
    
    if [ $CURL_EXIT_CODE -eq 0 ]; then
        HTTP_CODE=$(tail -n1 response.tmp)
        if [ "$HTTP_CODE" = "200" ]; then
            RESULT_COUNT=$(head -n -1 response.tmp | grep -o '"total":[0-9]*' | grep -o '[0-9]*')
            echo "  ✅ 成功: ${DURATION}ms, 结果数: $RESULT_COUNT"
        else
            echo "  ❌ 失败: HTTP $HTTP_CODE (${DURATION}ms)"
        fi
    else
        echo "  ❌ 超时: ${DURATION}ms"
    fi
done

rm -f response.tmp
echo ""

# 测试用例4: 系统稳定性测试
echo "4. 系统稳定性测试（快速连续请求）"
echo "---------------------------------------"

STABLE_PARAMS="province=辽宁&score=500&ranks=30000&year=2024&classify=物理类&subjects=物理&batch=本科批&pageNum=1&pageSize=100"

STABLE_SUCCESS=0
STABLE_TOTAL=5

for i in $(seq 1 $STABLE_TOTAL); do
    START_TIME=$(date +%s%3N)
    timeout 20s curl -s -w "\n%{http_code}" -X GET "${BASE_URL}${API_ENDPOINT}?${STABLE_PARAMS}" > response.tmp 2>&1
    CURL_EXIT_CODE=$?
    END_TIME=$(date +%s%3N)
    DURATION=$((END_TIME - START_TIME))
    
    if [ $CURL_EXIT_CODE -eq 0 ]; then
        HTTP_CODE=$(tail -n1 response.tmp)
        if [ "$HTTP_CODE" = "200" ]; then
            STABLE_SUCCESS=$((STABLE_SUCCESS + 1))
            echo "  请求 $i: ✅ ${DURATION}ms"
        else
            echo "  请求 $i: ❌ HTTP $HTTP_CODE"
        fi
    else
        echo "  请求 $i: ❌ 超时/失败"
    fi
    
    # 短暂间隔
    sleep 1
done

rm -f response.tmp

echo "稳定性测试结果: $STABLE_SUCCESS/$STABLE_TOTAL"
if [ "$STABLE_SUCCESS" -ge 4 ]; then
    echo "✅ 系统稳定性良好"
else
    echo "❌ 系统稳定性有问题"
fi
echo ""

# 总结
echo "=== 深度修复测试总结 ==="
echo "1. 极大数据量测试: 验证44823条数据场景是否修复"
echo "2. 内存压力测试: 验证连续大数据量请求的稳定性"
echo "3. 数据量递增测试: 验证不同数据量级别的处理能力"
echo "4. 系统稳定性测试: 验证快速连续请求的稳定性"
echo ""
echo "修复验证要点:"
echo "- 不应该出现StackOverflowError"
echo "- 响应时间应该在合理范围内（<30秒）"
echo "- 系统应该能稳定处理连续请求"
echo "- 内存使用应该可控"
echo ""
echo "如果测试通过，说明深度修复成功。"
echo "如果仍有问题，请检查应用日志中的详细信息。"
